package com.nth.rooster.authentication.dao;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nth.rooster.core.entities.RamUserCircleMember;
import com.nth.rooster.core.entities.RamLogins;
import com.nth.rooster.core.entities.RamRoles;
import com.nth.rooster.core.entities.RamUsers;
import com.nth.rooster.core.utils.DBUtils;

@Component
public class UserDao implements UserDaoI {

	static Logger LOGGER = Logger.getLogger(UserDao.class);
	
	@Autowired
	private SessionFactory factory;

	public List<RamUsers> getUsersBasedOnActiveStatus(Boolean isActive){
		
		Session session = factory.openSession();
		
		Query query = session.createQuery("from  RamUsers where active=:active");
		query.setBoolean("active", isActive);
		List<RamUsers> list = query.list();
		
		DBUtils.closeSession(session);
		return list;
	}
	
	public RamUsers saveRegistration(RamUsers ramUsers){
		Session session = factory.openSession();
		Transaction transaction = session.getTransaction();
		transaction.begin();
		if(ramUsers.getUserId()== null){
			Long ID= (Long)session.save(ramUsers);	
			ramUsers.setUserId(ID);
		}else{
			session.update(ramUsers);
		}
		transaction.commit();
		DBUtils.closeSession(session);
		return ramUsers;
	}
	
	public void saveCircleMapping(RamUserCircleMember ramCircleUser){
		Session session = factory.openSession();
		Transaction transaction = session.getTransaction();
		transaction.begin();
		session.save(ramCircleUser);
		
		transaction.commit();
		DBUtils.closeSession(session);
	}
	
	public RamUsers getUserData(Long userID) {
		LOGGER.debug("Entered::Authentication::UserDao::getUserData::userID" + userID);
		Session session= factory.openSession();
		RamUsers ramUsers = (RamUsers)session.get(RamUsers.class, userID);
		if (ramUsers!= null) {
			ramUsers.getRamRoles();
			for (RamUserCircleMember circleUser : ramUsers.getMemberForcircles()) {
				circleUser.getCircleID();
			}
			
			/**
			 * @TODO
			 * 
			 * Please fetch owner circle details also. 
			 */
		}
		
		DBUtils.closeSession(session);
		LOGGER.debug("Exit::Authentication::UserDao::getUserData::userID" + userID);
		return ramUsers;
		
	}
	
	public void saveLoginCredentials(RamLogins ramLogins){
		Session session = factory.openSession();
		Transaction transaction = session.getTransaction();
		transaction.begin();
		if(ramLogins.getLoginID()== null){
			session.save(ramLogins);
		}else{
			session.update(ramLogins);
		}
		transaction.commit();
		DBUtils.closeSession(session);
	}

	public Set<RamRoles> getUserMenu(Long userID) {
		Session session = factory.openSession();

		RamUsers ramUsers = (RamUsers)session.get(RamUsers.class, userID);
		if (ramUsers != null && ramUsers.getRamRoles() != null) {
			
			for (RamRoles roles :ramUsers.getRamRoles() ) {
				roles.getRamMenus();
				
				System.out.println(roles.getRamMenus().size());
			}
		}
		 
		
		DBUtils.closeSession(session);
		return ramUsers.getRamRoles();
	}
}
